<template>
  <div class="header-nav-menu">
    <div
      v-if="widget.type === 'header-menu'"
      class="grid-cell header-container"
      v-bind="layoutProps"
      :class="[ isMicroApp ? 'microAppEn': '',customClass]"
      :style="boxStyle"
      :key="widget.id"
      @click.stop="selectWidget(widget)"
    >
      <div class="avue-top">
        <template v-for="(subWidget, swIdx) in widget.widgetList">
          <template v-if="'container' === subWidget.category">
            <component
              :is="subWidget.type + '-widget'"
              :widget="subWidget"
              :designer="designer"
              :key="subWidget.id"
              :parent-list="widget.widgetList"
              :index-of-parent-list="swIdx"
              :parent-widget="widget"
            >
            </component>
          </template>
          <template v-else>
            <component
              :is="subWidget.type + '-widget'"
              :field="subWidget"
              :designer="designer"
              :key="subWidget.id"
              :parent-list="widget.widgetList"
              :index-of-parent-list="swIdx"
              :parent-widget="widget"
              :design-state="true"
            >
            </component>
          </template>
        </template>
      </div>
    </div>
<div class="avue-layout">
      <div class="avue-left">
        <!-- 左侧导航栏 -->
        <sidebar />
      </div>
      <div class="avue-main">
        <!-- 顶部标签卡 -->
        <tags />
        <transition name="fade-scale">
          <search class="avue-view" v-show="isSearch"></search>
        </transition>
        <!-- 主体视图层 -->
        <div
          style="height: 100%; overflow-y: auto; overflow-x: hidden"
          id="avue-view"
        >
          <router-view ref="com" class="avue-view" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Draggable from "vuedraggable";
import i18n from "@/utils/i18n";
import FieldComponents from "@/components/form-designer/form-widget/field-widget/index";
import refMixinDesign from "@/components/form-designer/refMixinDesign";
import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper";
import tags from "./components/tags";
import sidebar from "./sidebar/";

export default {
  name: "header-menu-item",
  componentName: "BoxContainerWidget",
  mixins: [i18n, refMixinDesign],
  inject: ["refList"],
  components: {
    tags,
    sidebar,
    Draggable,
    ContainerWrapper,
    ...FieldComponents,
  },
  props: {
    widget: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,

    colHeight: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      layoutProps: {
        span: this.widget.options.span || 12,
        // md: this.widget.options.md || 12,
        // sm: this.widget.options.sm || 12,
        // xs: this.widget.options.xs || 12,
        offset: this.widget.options.offset || 0,
        push: this.widget.options.push || 0,
        pull: this.widget.options.pull || 0,
      },
      isMicroApp: false, // 用于判断是不是微前端环境
    };
  },
  computed: {
    customClass() {
      return this.widget.options.customClass || "";
    },

    boxStyle() {
      const initStyle = this.widget.options.initStyle || {};
      if (!initStyle) {
        return {};
      }
      const style = {
        width:
          initStyle.width && initStyle.width.indexOf("%") > -1
            ? initStyle.width
            : initStyle.width + "px",
        // height:initStyle.height&&initStyle.height.indexOf('%') > -1 ? initStyle.height : initStyle.height + 'px',
        minHeight:
          initStyle.minHeight && initStyle.minHeight.indexOf("%") > -1
            ? initStyle.height
            : initStyle.height + "px",
        backgroundColor: initStyle.backgroundColor,
        color: initStyle.color,
        margin: this.getNumber(initStyle.margin),
        padding: this.getNumber(initStyle.padding),
        textAlign: initStyle.textAlign,
      };
      return style;
    },
  },

  created() {
    this.initRefList();
    this.initLayoutProps();
    this.isMicroApp = window.__MICRO_APP_ENVIRONMENT__;
  },
  methods: {
    getNumber(num) {
     if (Number(num) || Number(num)===0) {
        return `${num}px`;
      }
      if (num.indexOf(",") > -1) {
        const newData = num.split(",");
        const newDataPx = newData.map((v) => `${v}px`);
        return newDataPx.join(" ");
      }

      if (isNaN(Number(num))) {
        return 0;
      }
      return `${num}px`;
    },
    initLayoutProps() {
      if (this.widget.options.responsive) {
        let lyType = this.designer.formConfig.layoutType;
        if (lyType === "H5") {
          this.layoutProps.span = this.widget.options.xs || 12;
        } else if (lyType === "Pad") {
          this.layoutProps.span = this.widget.options.sm || 12;
        } else {
          this.layoutProps.span = this.widget.options.md || 12;
        }
      } else {
        this.layoutProps.span = this.widget.options.span;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.header-nav-menu {
  padding-left: 240px;
  position: relative;
  min-height: 300px;

}
.microAppEn{
  .avue-left {
    position: absolute;
    height: 100%;
  }
}
.header-container {
  width: 100%;
  min-height: 38px;
  //margin: 6px 0;  /* 设置了margin，栅格列的offset、push、pull会失效！！ */
  padding: 10px;
  box-sizing: border-box;
  position: relative;
  //border: 1px dashed rgba(170, 170, 170, 0.75);
  &.selected {
    border: 1px solid $--color-primary;
  }
  .form-widget-list {
    min-height: 46px;
  }

  .grid-col-action {
    position: absolute;
    bottom: 0;
    right: -2px;
    height: 28px;
    line-height: 28px;
    background: $--color-primary;
    z-index: 999;

    i {
      font-size: 14px;
      color: #fff;
      margin: 0 5px;
      cursor: pointer;
    }
  }

  .grid-col-handler {
    position: absolute;
    top: -2px;
    left: -2px;
    height: 22px;
    line-height: 22px;
    background: $--color-primary;
    z-index: 9;

    i {
      font-size: 14px;
      font-style: normal;
      color: #fff;
      margin: 4px;
      cursor: default;
    }
  }
}
.header-nav-main {
  width: 100%;
  height: 300px;
  line-height: 300px;
  text-align: center;
  background: #ccc;
}
</style>
